package com.xy.xymall.order.service.impl;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.xy.xymall.order.entity.OrderEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.common.utils.PageUtils;
import com.xy.common.utils.Query;

import com.xy.xymall.order.dao.OrderItemDao;
import com.xy.xymall.order.entity.OrderItemEntity;
import com.xy.xymall.order.service.OrderItemService;


@Service("orderItemService")
@RabbitListener(queues = {"hello-java-queue"})
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * @param message
     * @author XY
     * @date 2020/11/2 14:27
     * @return void
     * queues:声明要监听的所有队列
     * 发送的是什么类型，这里接收的就可以写什么类型
     * Message message:原生消息详细信息，头+体
     * Channel channel 当前传输数据的通道
     *
     */
//    @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    public void receieveMessage(Message message, OrderItemEntity itemEntity, Channel channel) {
        //channel内按顺序自增的
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //签收货物，非批量模式
        try {
            if(deliveryTag%2==0){
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了");
            }else{
                //requeue为false则丢弃，true则会重发回服务器，重新入队
                channel.basicNack(deliveryTag,false,true);
//                channel.basicReject();
                System.out.println("没签收");
            }
        } catch (IOException e) {
            //网络中断
            e.printStackTrace();
        }
        System.out.println("接收到消息。。。"+itemEntity.getSkuName());
    }

    @RabbitHandler
    public void receieveMessage(OrderEntity message){
        System.out.println("接收到消息。。。"+message.getReceiverName());
    }

}